
clear 
set more off
 
 
*  Generate change in immigrants inflows over time

 
use "$data\Q_data_country.dta" , clear
sort  nationality cod_prov year

keep if year<=2001 

gen mn2001 =mmn if year==2001
gen mn2000 =mmn if year==2000
gen mn1999 =mmn if year==1999
gen mn1998 =mmn if year==1998
gen mn1997 =mmn if year==1997
gen mn1996 =mmn if year==1996
gen mn1995 =mmn if year==1995

bys nationality cod_prov : egen M2001= max(mn2001)
bys nationality cod_prov : egen M2000= max(mn2000)
bys nationality cod_prov : egen M1999= max(mn1999)
bys nationality cod_prov : egen M1998= max(mn1998)
bys nationality cod_prov : egen M1997= max(mn1997)
bys nationality cod_prov : egen M1996= max(mn1996)
bys nationality cod_prov : egen M1995= max(mn1995)

gen Mdelta_all= M2001-M1995 
replace Mdelta_all= M2001-M1996 if Mdelta_all==. & M1996!=. 
replace Mdelta_all= M2001-M1997 if Mdelta_all==. & M1997!=. 
replace Mdelta_all= M2001-M1998 if Mdelta_all==. & M1998!=. 
replace Mdelta_all= M2001-M1999 if Mdelta_all==. & M1999!=. 
replace Mdelta_all= M2001-M2000 if Mdelta_all==. & M2000!=. 

replace Mdelta_all= M2000-M1995 if Mdelta_all==. & M1995!=. & M2000!=.
replace Mdelta_all= M2000-M1996 if Mdelta_all==. & M1996!=. & M2000!=.
replace Mdelta_all= M2000-M1997 if Mdelta_all==. & M1997!=. & M2000!=.
replace Mdelta_all= M2000-M1998 if Mdelta_all==. & M1998!=. & M2000!=.
replace Mdelta_all= M2000-M1999 if Mdelta_all==. & M1999!=. & M2000!=.

replace Mdelta_all= M1999-M1995 if Mdelta_all==. & M1995!=. & M1999!=.
replace Mdelta_all= M1999-M1996 if Mdelta_all==. & M1996!=. & M1999!=.
replace Mdelta_all= M1999-M1997 if Mdelta_all==. & M1997!=. & M1999!=.
replace Mdelta_all= M1999-M1998 if Mdelta_all==. & M1998!=. & M1999!=.

replace Mdelta_all= M1998-M1995 if Mdelta_all==. & M1995!=. & M1998!=.
replace Mdelta_all= M1998-M1996 if Mdelta_all==. & M1996!=. & M1998!=.
replace Mdelta_all= M1998-M1997 if Mdelta_all==. & M1997!=. & M1998!=.

replace Mdelta_all= M1997-M1995 if Mdelta_all==. & M1995!=. & M1997!=.
replace Mdelta_all= M1997-M1996 if Mdelta_all==. & M1996!=. & M1997!=.


gen fn2001 =fmn if year==2001
gen fn2000 =fmn if year==2000
gen fn1999 =fmn if year==1999
gen fn1998 =fmn if year==1998
gen fn1997 =fmn if year==1997
gen fn1996 =fmn if year==1996
gen fn1995 =fmn if year==1995

bys nationality cod_prov : egen F2001= max(fn2001)
bys nationality cod_prov : egen F2000= max(fn2000)
bys nationality cod_prov : egen F1999= max(fn1999)
bys nationality cod_prov : egen F1998= max(fn1998)
bys nationality cod_prov : egen F1997= max(fn1997)
bys nationality cod_prov : egen F1996= max(fn1996)
bys nationality cod_prov : egen F1995= max(fn1995)

gen Fdelta_all= F2001-F1995 
replace Fdelta_all= F2001-F1996 if Fdelta_all==. & F1996!=. 
replace Fdelta_all= F2001-F1997 if Fdelta_all==. & F1997!=. 
replace Fdelta_all= F2001-F1998 if Fdelta_all==. & F1998!=. 
replace Fdelta_all= F2001-F1999 if Fdelta_all==. & F1999!=. 
replace Fdelta_all= F2001-F2000 if Fdelta_all==. & F2000!=. 

replace Fdelta_all= F2000-F1995 if Fdelta_all==. & F1995!=. & F2000!=.
replace Fdelta_all= F2000-F1996 if Fdelta_all==. & F1996!=. & F2000!=.
replace Fdelta_all= F2000-F1997 if Fdelta_all==. & F1997!=. & F2000!=.
replace Fdelta_all= F2000-F1998 if Fdelta_all==. & F1998!=. & F2000!=.
replace Fdelta_all= F2000-F1999 if Fdelta_all==. & F1999!=. & F2000!=.

replace Fdelta_all= F1999-F1995 if Fdelta_all==. & F1995!=. & F1999!=.
replace Fdelta_all= F1999-F1996 if Fdelta_all==. & F1996!=. & F1999!=.
replace Fdelta_all= F1999-F1997 if Fdelta_all==. & F1997!=. & F1999!=.
replace Fdelta_all= F1999-F1998 if Fdelta_all==. & F1998!=. & F1999!=.

replace Fdelta_all= F1998-F1995 if Fdelta_all==. & F1995!=. & F1998!=.
replace Fdelta_all= F1998-F1996 if Fdelta_all==. & F1996!=. & F1998!=.
replace Fdelta_all= F1998-F1997 if Fdelta_all==. & F1997!=. & F1998!=.

replace Fdelta_all= F1997-F1995 if Fdelta_all==. & F1995!=. & F1997!=.
replace Fdelta_all= F1997-F1996 if Fdelta_all==. & F1996!=. & F1997!=.

sort nationality cod_prov year
bys nationality cod_prov : gen Mdelta= mmn[_n+1]-mmn
gen Mrate= Mdelta/Mdelta_all
bys nationality cod_prov : gen Fdelta= fmn[_n+1]-fmn
gen Frate= Fdelta/Fdelta_all
keep cod_prov year nationality Mrate Frate mmn fmn 
recode nationality (998=0) 
gen bc= nationality 
rename cod_prov provres
save "$data\census1991_share.dta" , replace



* Use single data from Census 2001, 1991: males and females

use "$data\Msinglemag_2001_country.dta" , clear
merge 1:1 provres hbc using "$data\Msinglemag_1991_country.dta" , keep(match )
tab _merge
drop _merge
gen Mdelta_singleall= (msinglemag_2001-msinglemag_1991)*(70/100)
cap drop year
save "$data\Mdelta_single_2001-1991.dta"  , replace

use "$data\census1991_share.dta" , clear
rename bc hbc
merge m:1 provres hbc using "$data\Mdelta_single_2001-1991.dta" , keep(match)
tab _merge
drop _merge
gsort provres hbc -year
gen Mdelta_single= Mdelta_singleall*Mrate
gen Msingle= msinglemag_2001 if year==2001
replace Msingle= Msingle[_n-1] - Mdelta_single if year==2000
replace Msingle= Msingle[_n-1] - Mdelta_single if year==1999
replace Msingle= Msingle[_n-1] - Mdelta_single if year==1998
replace Msingle= Msingle[_n-1] - Mdelta_single if year==1997
replace Msingle= Msingle[_n-1] - Mdelta_single if year==1996
replace Msingle= Msingle[_n-1] - Mdelta_single if year==1995
order provres year hbc Msingle msinglemag_2001 msinglemag_1991
drop if year==2001
save "$data\Msinglemag_country_1995-2000.dta" , replace


use "$data\Fsinglemag_2001_country.dta" , clear
merge 1:1 provres wbc using "$data\Fsinglemag_1991_country.dta" , keep(match )
tab _merge
drop _merge
gen Fdelta_singleall= (fsinglemag_2001-fsinglemag_1991)*(70/100)
cap drop year
save "$data\Fdelta_single_2001-1991.dta"  , replace

use "$data\census1991_share.dta" , clear
rename bc wbc
merge m:1 provres wbc using "$data\Fdelta_single_2001-1991.dta" , keep(match)
tab _merge
drop _merge
gsort provres wbc -year
gen Fdelta_single= Fdelta_singleall*Frate
gen Fsingle= fsinglemag_2001 if year==2001
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==2000
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==1999
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==1998
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==1997
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==1996
replace Fsingle= Fsingle[_n-1] - Fdelta_single if year==1995
order provres year wbc Fsingle fsinglemag_2001 fsinglemag_1991
drop if year==2001
save "$data\Fsinglemag_country_1995-2000.dta" , replace







